﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.Model;
using OgilvyOne.BLL;
using OgilvyOne.Utility;

public partial class admin_infomation_info_img_detail : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            imgsmalltr.Visible = false;
            imglisttr.Visible = false;
            int aid = int.Parse(Request.Params["aid"]);

            CmsArticleModel cm = new CmsArticleBll().GetCmsArticleModel(aid);
            this.headers.Text = "当前位置：" + cm.Title + "&nbsp;&nbsp;<img src='../images/subnav_dot.gif' />&nbsp;&nbsp;图片详情";

            if (Request.Params["id"] != null && Request.Params["id"] != "")
            {
                int id = int.Parse(Request.Params["id"]);
                this.id.Value = id.ToString();
                CmsArticleImgModel im = new CmsArticleImgsBll().GetImgModel(id);
                this.img_file_name.Value = im.ImgFileName;
                this.img_name.Text = im.ImgName;
                this.img_order.Value = im.ImgOrder.ToString();
                this.state.Text = im.State.ToString();
                this.typelist.Text = im.ImgType.ToString();
                this.img_small_file_name.Value = im.ImgSmallFileName;
                this.img_list_file_name.Value = im.Img_list_file_name;

                if (im.ImgFileName != null && im.ImgFileName != "")
                {
                    this.img_file_name_img.Src = path + ConConstants.UPLOADFILES_IMAGE_PATH + im.ImgFileName;
                }
                else
                {
                    this.img_file_name_img.Visible = false;
                }
                if (im.Img_list_file_name != null && im.Img_list_file_name != "")
                {
                    this.img_list_file_name_img.Src = path + ConConstants.UPLOADFILES_IMAGE_PATH + im.Img_list_file_name;
                }
                else
                {
                    this.img_list_file_name_img.Visible = false;
                }
                if (im.ImgSmallFileName != null && im.ImgSmallFileName != "")
                {
                    this.img_small_file_name_img.Src = path + ConConstants.UPLOADFILES_IMAGE_PATH + im.ImgSmallFileName;
                }
                else
                {
                    this.img_small_file_name_img.Visible = false;
                }
            }
            else
            {
                this.img_order.Value = (new CmsArticleImgsBll().MaxOrder(aid) + 1).ToString();
                this.img_list_file_name_img.Visible = false;
                this.img_file_name_img.Visible = false;
                this.img_small_file_name_img.Visible = false;
            }

            if (this.typelist.Text == CmsConstants.IMG_TYPE_COMMON.ToString())
            {
                imgsmalltr.Visible = true;
                imglisttr.Visible = true;
            }
        }
    }

    protected void Sub_click(object sender, EventArgs e)
    {
        bool isup = false;
        if (this.id.Value != null && this.id.Value != "")
        {//update
            this.PopedomValidate(Request.Params["layerid"], MOD);
            CmsArticleImgModel uim = GetPageModel();
            
            isup = new CmsArticleImgsBll().Update(uim);
            insertLog("修改文章图片:" + uim.ImgName);
        }
        else
        {//add
            this.PopedomValidate(Request.Params["layerid"], ADD);
            CmsArticleImgModel uim = GetPageModel();
            uim.ArticleId = int.Parse(Request.Params["aid"]);
            uim.UserId = GetSessionUsersModel().Aid;
            uim.Add_time = DateTime.Now;
            isup = new CmsArticleImgsBll().Add(uim) > 1;
            insertLog("新增文章图片:" + uim.ImgName);
        }
        if (isup)
        {
            if (this.typelist.Text == "1")
            {//题头图更新到内容表中,方便列表查询
                CmsArticleBll cabll = new CmsArticleBll();
                CmsArticleModel cm = cabll.GetCmsArticleModel(int.Parse(Request.Params["aid"]));
                cm.Titleimg = this.img_file_name.Value;
                cm.Modifytime = DateTime.Now;
                cm.Modifyusername = GetSessionUsersModel().Username;
                cabll.Update(cm);
            }
            new CmsArticleBll().UpdateAudit(int.Parse(Request.Params["aid"]), GetSessionUsersModel().Username);
        }
        Response.Redirect("info_img_list.aspx?aid=" + Request.Params["aid"] + "&layerid=" + Request.Params["layerid"]);
    }

    private CmsArticleImgModel GetPageModel()
    {
        CmsArticleImgModel aim = new CmsArticleImgModel();
        if (this.id.Value != null && this.id.Value != "")
        {
            aim.ImgsId = int.Parse(this.id.Value);
        }
        aim.ImgName = this.img_name.Text;
        aim.ImgFileName = this.img_file_name.Value;
        aim.ImgType = int.Parse(this.typelist.Text);
        if (this.img_order.Value != "")
        {
            aim.ImgOrder = int.Parse(this.img_order.Value);
        }
        aim.ImgSmallFileName = this.img_small_file_name.Value;
        aim.Img_list_file_name = this.img_list_file_name.Value;
        aim.State = int.Parse(this.state.Text);
        return aim;
    }
    protected void typelist_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.typelist.SelectedValue == CmsConstants.IMG_TYPE_COMMON.ToString())
        {
            imgsmalltr.Visible = true;
            imglisttr.Visible = true;
        }
        else
        {
            imgsmalltr.Visible = false;
            imglisttr.Visible = false;
        }
    }
}
